package com.jtny.nytb.equipment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jtny.component.enums.GenericStatusEnum;
import com.jtny.component.util.BeanUtils;
import com.jtny.nytb.equipment.dao.araetype.DeviceAreaDAO;
import com.jtny.nytb.equipment.dao.equipment.EquipmentGatewayRelationDAO;
import com.jtny.nytb.equipment.dao.equipment.EquipmentInfoDAO;
import com.jtny.nytb.equipment.domain.areatype.DeviceAreaDO;
import com.jtny.nytb.equipment.domain.equipment.EquipmentGatewayRelationDO;
import com.jtny.nytb.equipment.domain.equipment.EquipmentInfoDO;
import com.jtny.nytb.equipment.dto.EquipmentDTO;
import com.jtny.nytb.equipment.dto.PowerMonitorDTO;
import com.jtny.nytb.equipment.dto.device.EquipmentGatewayRelationDTO;
import com.jtny.nytb.equipment.service.PowerMonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 智慧能源中心 - 车智斌
 * @package com.jtny.energymonitor.equipment.service.impl
 * @fileName PowerMonitorServiceImpl.java
 * @createTime 2023年10月11日 09:40
 * @Copyright© 2023 jtny
 * 江投能源技术研究院 版权所有
 */
@Service
public class PowerMonitorServiceImpl implements PowerMonitorService {

    @Autowired
    private DeviceAreaDAO deviceAreaDAO;

    @Autowired
    private EquipmentInfoDAO equipmentInfoDAO;

    @Autowired
    private EquipmentGatewayRelationDAO equipmentGatewayRelationDAO;

    @Override
    public List<PowerMonitorDTO> queryAll() {

        QueryWrapper<DeviceAreaDO> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("status", GenericStatusEnum.NORMAL.getValue());
        //查询出所有正常的配电监测信息
        List<DeviceAreaDO> deviceAreaDOS = deviceAreaDAO.selectList(queryWrapper);

        return BeanUtils.copyToList(deviceAreaDOS, PowerMonitorDTO.class);
    }

    @Override
    public List<EquipmentDTO> queryEquipments(PowerMonitorDTO powerMonitorDTO) {

        QueryWrapper<EquipmentInfoDO> queryWrapper = new QueryWrapper<>();

        if (powerMonitorDTO.getId() != null) {

            queryWrapper.eq("status", GenericStatusEnum.NORMAL.getValue()).eq("area_id", powerMonitorDTO.getId()).eq("is_measuring_point_device", 0);

            List<EquipmentInfoDO> equipmentDOS = equipmentInfoDAO.selectList(queryWrapper);

            return BeanUtils.copyToList(equipmentDOS, EquipmentDTO.class);
        } else
            return null;
    }

    @Override
    public List<EquipmentDTO> queryGWEquipmentsByAreaId(Long id) {

        QueryWrapper<EquipmentInfoDO> queryWrapper = new QueryWrapper<>();

        if (id != null) {

            queryWrapper.eq("status", GenericStatusEnum.NORMAL.getValue()).eq("area_id", id).eq("is_measuring_point_device", 1);

            List<EquipmentInfoDO> equipmentDOS = equipmentInfoDAO.selectList(queryWrapper);

            return BeanUtils.copyToList(equipmentDOS, EquipmentDTO.class);
        } else
            return null;
    }

    @Override
    public List<EquipmentDTO> queryAllEquipments() {

        QueryWrapper<EquipmentInfoDO> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("status", GenericStatusEnum.NORMAL.getValue()).eq("is_measuring_point_device", 0);

        List<EquipmentInfoDO> equipmentDOS = equipmentInfoDAO.selectList(queryWrapper);

        return BeanUtils.copyToList(equipmentDOS, EquipmentDTO.class);
    }

    @Override
    public List<EquipmentDTO> queryEquipmentsByArea(List<Long> list) {

        QueryWrapper<EquipmentInfoDO> queryWrapper = new QueryWrapper<>();

        if (!list.isEmpty()) {

            queryWrapper.eq("status", GenericStatusEnum.NORMAL.getValue()).in("area_id", list).eq("is_measuring_point_device", 0);

            List<EquipmentInfoDO> equipmentDOS = equipmentInfoDAO.selectList(queryWrapper);

            return BeanUtils.copyToList(equipmentDOS, EquipmentDTO.class);
        } else
            return null;
    }

    @Override
    public List<EquipmentDTO> queryGWEquipments() {

        QueryWrapper<EquipmentInfoDO> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("status", GenericStatusEnum.NORMAL.getValue()).eq("is_measuring_point_device", 1);

        List<EquipmentInfoDO> equipmentDOS = equipmentInfoDAO.selectList(queryWrapper);

        return BeanUtils.copyToList(equipmentDOS, EquipmentDTO.class);

    }

    @Override
    public List<EquipmentDTO> queryNormalEquipments() {

        QueryWrapper<EquipmentInfoDO> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("status", GenericStatusEnum.NORMAL.getValue()).eq("is_measuring_point_device", 0);

        List<EquipmentInfoDO> equipmentDOS = equipmentInfoDAO.selectList(queryWrapper);

        return BeanUtils.copyToList(equipmentDOS, EquipmentDTO.class);
    }

    @Override
    public List<EquipmentGatewayRelationDTO> queryNormalIdByGWId(String dockIdentifier) {

        QueryWrapper<EquipmentGatewayRelationDO> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("gw_id", dockIdentifier);

        List<EquipmentGatewayRelationDO> equipmentGatewayRelationDOS = equipmentGatewayRelationDAO.selectList(queryWrapper);

        return BeanUtils.copyToList(equipmentGatewayRelationDOS, EquipmentGatewayRelationDTO.class);
    }

    @Override
    public List<EquipmentDTO> queryGWEquipmentsByArea(List<Long> list) {

        QueryWrapper<EquipmentInfoDO> queryWrapper = new QueryWrapper<>();

        if (!list.isEmpty()) {

            queryWrapper.eq("status", GenericStatusEnum.NORMAL.getValue()).in("area_id", list).eq("is_measuring_point_device", 1);

            List<EquipmentInfoDO> equipmentDOS = equipmentInfoDAO.selectList(queryWrapper);

            return BeanUtils.copyToList(equipmentDOS, EquipmentDTO.class);
        } else
            return null;
    }

    @Override
    public List<EquipmentDTO> queryEquipmentsByGW() {

        List<EquipmentDTO> list = new ArrayList<>();

        QueryWrapper<EquipmentGatewayRelationDO> queryWrapper = new QueryWrapper<>();

        queryWrapper.isNotNull("gw_id").ne("gw_id","");

        List<EquipmentGatewayRelationDO> equipmentGatewayRelationDOS = equipmentGatewayRelationDAO.selectList(queryWrapper);

        if (!equipmentGatewayRelationDOS.isEmpty()){

            equipmentGatewayRelationDOS.stream().forEach(a -> list.add(getEquipmentByDockIdentifier(a.getMcbId())));
        }

        return list;
    }

    private EquipmentDTO getEquipmentByDockIdentifier(String dockIdentifier){

        QueryWrapper<EquipmentInfoDO> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("dock_identifier",dockIdentifier);

        EquipmentInfoDO equipmentInfoDO = equipmentInfoDAO.selectOne(queryWrapper);

        return BeanUtils.copyToObject(equipmentInfoDO,EquipmentDTO.class);
    }
}
